//
//  SettingDetailFeedBackViewController.m
//  宜居HD
//
//  Created by 陈柏兵 on 16/5/4.
//  Copyright © 2016年 张金国. All rights reserved.
//

#import "SettingDetailFeedBackViewController.h"
#import "Masonry.h"
#import "Macro.h"
#import "SettingDetailFeedBackViewModel.h"
#import "UIAlertControllerUtil.h"

#define FeedBack1 @"  欢迎吐槽~"
#define FeedBack2 @"  \n亲，留个联系方式吧！"

@interface SettingDetailFeedBackViewController ()<UITextViewDelegate>
@property (nonatomic, strong)UIButton *submitBtn;//提交按钮
@property (nonatomic, strong)UITextView *feedBackView;//吐槽意见框
@property (nonatomic, strong)UITextView *phoneView;//联系方式框

@property (nonatomic, strong)SettingDetailFeedBackViewModel *settingDetailFeedBackViewModel;
@end

@implementation SettingDetailFeedBackViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"意见反馈";
    
    self.view.backgroundColor = kSettingBackgroundColor;
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self layOutUI];
}

//masonry布局
- (void)layOutUI {
    [self submitBtn];
    
    [self.feedBackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).with.offset(75);
        make.left.equalTo(self.view.mas_left).offset(TweentyOffsetViewMargin);
        make.right.equalTo(self.view.mas_right).offset(-TweentyOffsetViewMargin);
        make.height.equalTo(self.view.mas_height).multipliedBy(2/5.0f);
    }];
    
    [self.phoneView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.feedBackView.mas_bottom).offset(TweentyOffsetViewMargin + TenOffsetViewMargin);
        make.left.equalTo(self.view.mas_left).offset(TweentyOffsetViewMargin);
        make.right.equalTo(self.view.mas_right).offset(-TweentyOffsetViewMargin);
        make.height.equalTo(self.view.mas_height).multipliedBy(1/13.0f);
    }];
}

#pragma mark - Getters
- (UIButton *)submitBtn {
    if (_submitBtn == nil) {
        _submitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _submitBtn.layer.cornerRadius = 5;
        _submitBtn.frame = CGRectMake(0, 7, 73, 30);
        _submitBtn.layer.borderColor = [[UIColor colorWithRed:212/255.0 green:215/255.0 blue:223/255.0 alpha:1.000] CGColor];
        [_submitBtn setTitle:@"提交" forState:0];
        [_submitBtn setTitleColor:[UIColor blackColor] forState:0];
        _submitBtn.layer.borderWidth = 0.6f;
        [_submitBtn addTarget:self action:@selector(handleSubmitAction:) forControlEvents:64];
        UIBarButtonItem *submitItem = [[UIBarButtonItem alloc] initWithCustomView:_submitBtn];
        self.navigationItem.rightBarButtonItem = submitItem;
    }
    return _submitBtn;
}

- (UITextView *)feedBackView {
    if (_feedBackView == nil) {
        _feedBackView = [[UITextView alloc] init];
        _feedBackView.backgroundColor=[UIColor whiteColor];
        _feedBackView.editable = YES;        //是否允许编辑内容，默认为“YES”
        _feedBackView.delegate = self;       //设置代理方法的实现类
        _feedBackView.font=[UIFont fontWithName:@"Arial" size:17.0]; //设置字体名字和字体大小;
        _feedBackView.returnKeyType = UIReturnKeyDefault;//return键的类型
        _feedBackView.keyboardType = UIKeyboardTypeDefault;//键盘类型
        _feedBackView.textAlignment = NSTextAlignmentLeft; //文本显示的位置默认为居左
        _feedBackView.dataDetectorTypes = UIDataDetectorTypeAll; //显示数据类型的连接模式（如电话号码、网址、地址等）
        _feedBackView.textColor = [UIColor blackColor];
        _feedBackView.layer.cornerRadius = 5;
        NSMutableAttributedString *feedBackAttrString = [[NSMutableAttributedString alloc] initWithString:FeedBack1];
        [feedBackAttrString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:0.7922 green:0.7922 blue:0.7922 alpha:1.0] range:NSMakeRange(0,feedBackAttrString.length)];
        _feedBackView.attributedText = feedBackAttrString;
        _feedBackView.tag = 200;
        [self.view addSubview:_feedBackView];
    }
    return _feedBackView;
}

- (UITextView *)phoneView {
    if (_phoneView == nil) {
        _phoneView = [[UITextView alloc] init];
        _phoneView.backgroundColor=[UIColor whiteColor];
        _phoneView.scrollEnabled = NO;    //当文字超过视图的边框时是否允许滑动，默认为“YES”
        _phoneView.editable = YES;        //是否允许编辑内容，默认为“YES”
        _phoneView.delegate = self;       //设置代理方法的实现类
        _phoneView.font=[UIFont fontWithName:@"Arial" size:17.0]; //设置字体名字和字体大小;
        _phoneView.returnKeyType = UIReturnKeyDefault;//return键的类型
        _phoneView.keyboardType = UIKeyboardTypeNumberPad;//键盘类型
        _phoneView.textAlignment = NSTextAlignmentLeft; //文本显示的位置默认为居左
        _phoneView.dataDetectorTypes = UIDataDetectorTypeAll; //显示数据类型的连接模式（如电话号码、网址、地址等）
        _phoneView.textColor = [UIColor blackColor];
        _phoneView.layer.cornerRadius = 5;
        NSMutableAttributedString *PhoneAttrString = [[NSMutableAttributedString alloc] initWithString:FeedBack2];
        [PhoneAttrString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:0.7922 green:0.7922 blue:0.7922 alpha:1.0] range:NSMakeRange(0,PhoneAttrString.length)];
        _phoneView.attributedText = PhoneAttrString;
        _phoneView.tag = 201;
        [self.view addSubview:_phoneView];
    }
    return _phoneView;
}

- (SettingDetailFeedBackViewModel *)settingDetailFeedBackViewModel {
    if (!_settingDetailFeedBackViewModel) {
        _settingDetailFeedBackViewModel = [SettingDetailFeedBackViewModel new];
        _settingDetailFeedBackViewModel.viewObject = self;
    }
    return _settingDetailFeedBackViewModel;
}

#pragma mark - Event Response
- (void)loadNetWork {
    [_phoneView.text isEqualToString:FeedBack2] == YES ? _phoneView.text = @"" : _phoneView.text;
    [_feedBackView.text isEqualToString:FeedBack2] == YES ? _feedBackView.text = @"" : _feedBackView.text;
    NSDictionary *dict = @{@"feedback":_feedBackView.text,
                           @"contact":_phoneView.text};
    self.settingDetailFeedBackViewModel.postObject = dict;
    [self.settingDetailFeedBackViewModel loadRequest];
    
}

- (void)handleSubmitAction:(UIButton *)button {
    if (![_phoneView.text isEqualToString:FeedBack2] || ![_feedBackView.text isEqualToString:FeedBack1]) {
        [self loadNetWork];
    } else {
        [UIAlertControllerUtil generateInfoControllerWithTitle:@"温馨提示" message:@"您还没有填写反馈意见哦"];
    }
    
}

#pragma mark - UITextView Delegate
- (void)textViewDidBeginEditing:(UITextView *)textView {
    //隐藏站位文字
    NSInteger TextTag = textView.tag - 200;
    switch (TextTag) {
        case 0:
            if ([textView.text isEqualToString:FeedBack1]) {
                textView.text = @"";
            }
            break;
            
        case 1:
            if ([textView.text isEqualToString:FeedBack2]) {
                textView.text = @"";
            }
            break;
            
        default:
            break;
    }
    
}

- (void)textViewDidEndEditing:(UITextView *)textView {
    //显示站位文字
    NSInteger TextTag = textView.tag - 200;
    switch (TextTag) {
        case 0:
            if (textView.text.length < 1) {
                NSMutableAttributedString *feedBackAttrString = [[NSMutableAttributedString alloc] initWithString:FeedBack1];
                [feedBackAttrString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:0.7922 green:0.7922 blue:0.7922 alpha:1.0] range:NSMakeRange(0,feedBackAttrString.length)];
                textView.attributedText = feedBackAttrString;
            }
            break;
            
        case 1:
            if (textView.text.length < 1) {
                NSMutableAttributedString *PhoneAttrString = [[NSMutableAttributedString alloc] initWithString:FeedBack2];
                [PhoneAttrString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:0.7922 green:0.7922 blue:0.7922 alpha:1.0] range:NSMakeRange(0,PhoneAttrString.length)];
                textView.attributedText = PhoneAttrString;
            }
            break;
            
        default:
            break;
    }
    
}


- (void)setConfigString:(NSString *)configString {
    _phoneView.text = FeedBack2;
    _feedBackView.text = FeedBack1;
    [UIAlertControllerUtil generateInfoControllerWithTitle:@"温馨提示" message:configString];
}


@end
